package com.zlp.dto.test;
import com.google.common.collect.Lists;

import com.zlp.utils.AES;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
public class SupplierCardCipherDTO implements Serializable {


    /**
     *  订单编号
     */
    private String orderNo;

    /**
     *  客户方订单编号
     */
    private String cusOrderNo;


    /**
     *  订单状态
     */
    private Integer orderState;

    /**
     *  订单状态描述
     */
    private String orderStateDesc;

    /**
     *  订单购买信息
     */
    private List<OrderBuyInfo> orderBuyInfo;


    @Data
    public static class CardCipherList implements Serializable {


        /**
         *  过期时间 yyyy-MM-dd
         */
        private String expiresDate;

        /**
         *  类型 1:卡号; 2:卡密
         */
        private Integer cardCipherType;

        /**
         *  加密后的卡密（使用 AES 加密，密钥同 APP 密钥）
         */
        private String cardCipherEnc;
        /**
         *  加密后的卡号（使用 AES加密，密钥同 APP 密钥）
         */
        private String cardNumberEnc;
    }

    /**
     * @Classname OrderBuyInfo
     * @Description 卡券订单购买信息
     * @Date 2022/9/26 17:29
     * @Created by ZouLiPing
     */
    @Data
    @NoArgsConstructor
    public static class OrderBuyInfo implements Serializable{

        /**
         *  商品SKU
         */
        private String skuNo;

        /**
         *  客户方商品SKU
         */
        private String cusSkuNo;

        /**
         *  购买数量
         */
        private Integer skuNum;

        /**
         *  卡券列表
         */
        private List<CardCipherList> cardCipherList;
    }
    public static SupplierCardCipherDTO conventSupplierCard(LhCardSupplierResp lhCardSupplierResp) {

        SupplierCardCipherDTO supplierCardCipherDTO = new SupplierCardCipherDTO();
        supplierCardCipherDTO.setOrderNo(lhCardSupplierResp.getSubOrderNo());
        List<LhCardSupplierResp.OrderItems> orderItems = lhCardSupplierResp.getOrderItems();
        List<OrderBuyInfo> orderBuyInfo = orderItems.stream().map(SupplierCardCipherDTO::conventOrderItems).collect(Collectors.toList());
        supplierCardCipherDTO.setOrderBuyInfo(orderBuyInfo);
        supplierCardCipherDTO.setOrderState(0);
        supplierCardCipherDTO.setCusOrderNo(lhCardSupplierResp.getOrderNo() );
        return supplierCardCipherDTO;
    }

    public static OrderBuyInfo conventOrderItems(LhCardSupplierResp.OrderItems orderItems) {
        OrderBuyInfo orderBuyInfo = new OrderBuyInfo();
        orderBuyInfo.setCusSkuNo(orderItems.getSkuNo());
        orderBuyInfo.setSkuNum(orderItems.getSkuNum());
        List<LhCardSupplierResp.CardItems> cardItems = orderItems.getCardItems();
        List<CardCipherList> cipherLists = cardItems.stream().map(SupplierCardCipherDTO::conventCardCipher)
                .collect(Collectors.toList());
        orderBuyInfo.setCardCipherList(cipherLists);
        return orderBuyInfo;
    }

    private static CardCipherList conventCardCipher(LhCardSupplierResp.CardItems cardItems) {

        CardCipherList cardCipherList = new CardCipherList();
        cardCipherList.setExpiresDate(cardItems.getEndTime());
        cardCipherList.setCardCipherType(Integer.parseInt(cardItems.getGoodsType()));
        // 产品类型 - 1.卡密账号 2.卡号密码 3.网址链接
        if (Objects.equals(cardItems.getGoodsType(),"1")) {
            cardCipherList.setCardNumberEnc(cardItems.getGoodsCard());
            cardCipherList.setCardCipherEnc(cardItems.getExchangeUrl());
        } else if (Objects.equals(cardItems.getGoodsType(),"2")){
            cardCipherList.setCardNumberEnc(cardItems.getGoodsCard());
            cardCipherList.setCardCipherEnc(cardItems.getGoodsPwd());
        }else if (Objects.equals(cardItems.getGoodsType(),"3")){
            cardCipherList.setCardNumberEnc(cardItems.getGoodsLink());
            cardCipherList.setCardCipherEnc(cardItems.getExchangeUrl());
        }
        return cardCipherList;
    }

}
